<template>
  <el-dialog
    :title="category.id ? '编辑分类' : '添加分类'"
    :visible="isShowDialog"
    @close="closeFn"
  >
    <el-form
      label-width="80px"
      :model="category"
      ref="categoryForm"
      :rules="categoryRules"
    >
      <el-form-item prop="title" label="标题">
        <el-input v-model="category.title"></el-input>
      </el-form-item>
      <el-form-item prop="desc" label="描述">
        <el-input v-model="category.desc"></el-input>
      </el-form-item>
      <el-form-item label="封面:" prop="image">
        <UploadImage ref="UploadImage" />
      </el-form-item>
      <el-form-item align="center">
        <el-button class="btnCancel" @click="closeFn">取消</el-button>
        <el-button type="primary" @click="onSubimt" class="btnOk"
          >确定</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addCategory, getCategoryById, editCategory } from "@/api/category";
export default {
  data() {
    return {
      isShowDialog: false,
      category: {
        title: "",
        desc: "",
        image: "",
      },
      categoryRules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        desc: [{ required: true, message: "描述不能为空", trigger: "blur" }],
        image:[{required:true,message:'图片不能为空',trigger:"change"}]
      },
    };
  },
  methods: {
    closeFn() {
      this.category = {
        title: "",
        desc: "",
        image: "",
      };
      this.$refs.UploadImage.fileList = [];
      this.isShowDialog = false;
    },
    getFileList(fileList) {
      this.category.image = fileList;
    },
    async onSubimt() {
      try {
        
      // 获取图片路径
      this.category.image = this.$refs.UploadImage.fileList[0]?.url;
      this.$refs.categoryForm.validateField('image')
      // 表单验证
      await this.$refs.categoryForm.validate();
      if (this.category.id) {
        await editCategory(this.category);
      } else {
        // 发送请求
        await addCategory(this.category);
        // 清空表单
        this.category = {
          title: "",
          desc: "",
          image: "",
        };
      }

      this.$refs.categoryForm.resetFields();
      // 提示信息
      this.$message.success("新增成功");
      this.isShowDialog = false;
      this.$emit("updateCategory");
      } catch (error) {
        console.log(error);
      }
    },
    // 获取指定分类
    async editCategory(id) {
      // 发送请求
      this.category = await getCategoryById(id);
      setTimeout(() => {
        // 清空子组件的文件数组数据
        this.$refs.UploadImage.fileList = [];
        // 将获取指定分类的图片推进去
        this.$refs.UploadImage.fileList.push({
          name: this.category.title,
          url: this.category.image,
        });
      this.$refs.categoryForm.validateField('image')
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-dialog {
  .el-dialog__header {
    background-color: #66ccff;
    span,
    .el-dialog__close {
      color: #fff;
    }
  }
}

.btnCancel {
  margin-right: 50px;
}
.btnOk {
  margin-left: 50px;
}
</style>